/*
 * @Author: huangch
 * @Date: 2023-11-13 21:39:11
 * @Description: 
 */
import { Decimal } from "decimal.js"
import { keepTwoDecimal } from '@/utils/common'
export const changeItemPrice = (record, changeParam) => {
  let discountedAmount = parseFloat(record.discountedAmount)
  let discountedRate = parseFloat(record.discountedRate || 0)
  let totalAmount = parseFloat(record.itemSubtotalAmount || 0)
  let originalTotalAmt = parseFloat((!record.itemOriginalTotalAmt && record.itemOriginalTotalAmt !== 0 ?  record.itemSubtotalAmount : record.itemOriginalTotalAmt) || 0)
  let price = parseFloat(record.sellItemPrice)
  let originalPrice = parseFloat(record.originalItemPrice)
  let num = parseFloat(record.itemTime)
  if (changeParam == 'totalAmount') {
    if(totalAmount > originalTotalAmt) {
      originalTotalAmt = totalAmount;
    }
    // 若修改总价，则同步修改 优惠率、优惠金额、单价
    discountedAmount =  originalTotalAmt - totalAmount
    discountedRate = originalTotalAmt == 0 ? 0 : (discountedAmount / originalTotalAmt) * 100
    price = num == 0 ? 0 : totalAmount / num
  } else if (changeParam == 'discountedRate') {
    // 若修改优惠率，则同步修改 总价、优惠金额、单价
    // @ts-ignore
    totalAmount = new Decimal(1 - (discountedRate / 100)).mul(new Decimal(originalTotalAmt)).toNumber();
    // totalAmount = (1 - (discountedRate / 100)) * originalTotalAmt
    discountedAmount = originalTotalAmt - totalAmount
    price = num == 0 ? 0 : totalAmount / num
  } else if (changeParam == 'price') {
    // 若修改单价，则同步修改 总价、优惠金额、优惠率
    totalAmount = price * num
    if(totalAmount > originalTotalAmt) {
      originalTotalAmt = totalAmount;
    }
    discountedAmount = originalTotalAmt - totalAmount
    discountedRate =  originalTotalAmt == 0 ? 0 : (discountedAmount / originalTotalAmt) * 100
  } else if (changeParam == 'itemTime') {
    // 若修改数量，则同步修改原始总价、总价、优惠金额
    totalAmount = price * num
    originalTotalAmt = originalPrice * num
    discountedAmount = originalTotalAmt - totalAmount
  } else if( changeParam == 'originalTotalAmount') {
    console.log('originalTotalAmt',originalTotalAmt)
    // 若修改金额（原始），则将优惠则为0，同步修改小计&销售单价
    discountedAmount = 0
    discountedRate = 0
    originalPrice = num == 0 ? 0 : originalTotalAmt / num
    price = originalPrice
    totalAmount = originalTotalAmt;
  }
  if(totalAmount > originalTotalAmt) {
    originalTotalAmt = totalAmount;
  }
  record.discountedRate = keepTwoDecimal(discountedRate)
  record.itemSubtotalAmount = keepTwoDecimal(totalAmount)
  record.itemOriginalTotalAmt = keepTwoDecimal(originalTotalAmt)
  
  record.discountedAmount = keepTwoDecimal(new Decimal(parseFloat(record.itemOriginalTotalAmt)).sub(new Decimal(parseFloat(record.itemSubtotalAmount))).toNumber());
  record.sellItemPrice = keepTwoDecimal(price)
  record.originalItemPrice = (!originalPrice && originalPrice !== 0 ? price : originalPrice) || 0
  record.originalItemPrice = keepTwoDecimal(originalPrice < price ? price : originalPrice) // 保持原价不能小于销价
  console.log(record, 'item record');
}
// @
export const  changePartPrice = (record, changeParam) => {
  let discountedAmount = parseFloat(record.discountedAmount) ;
  let discountedRate = parseFloat(record.discountedRate || 0);
  let totalAmount = parseFloat(record.partSubtotalAmount || 0);
  let price = parseFloat(record.sellPartPrice || 0);
  let originalPrice = parseFloat(record.originalPartPrice)
  let num = parseFloat(record.partQty || 0);
  let originalTotalAmt = num * price
  
  if(changeParam == 'totalAmount') {
    if(totalAmount > originalTotalAmt) {
      originalTotalAmt = totalAmount;
      price = num == 0 ? 0 : originalTotalAmt / num;
    }
    // 若修改总价，则同步修改 优惠率、优惠金额、单价
    discountedAmount= originalTotalAmt - totalAmount; 
    discountedRate = originalTotalAmt == 0 ? 0 : discountedAmount / originalTotalAmt * 100
  } else if( changeParam == 'discountedRate') {
    // 若修改优惠率，则同步修改优惠金额、小计
    // @ts-ignore
    totalAmount = new Decimal(1 - (discountedRate / 100)).mul(new Decimal(originalTotalAmt)).toNumber();
    discountedAmount = originalTotalAmt - totalAmount;
  } else if ( changeParam == 'price' || changeParam == 'itemTime') {
    // 若修改单价，同步修改金额，小计; 而优惠金额、优惠率则设置为0
    originalTotalAmt = price * num;
    totalAmount = originalTotalAmt;
    discountedAmount= 0;
    discountedRate = 0;
  }else if( changeParam == 'originalTotalAmount') {
    // 若修改优惠金额，则同步修改 总价、优惠率、单价
    discountedAmount = originalTotalAmt - totalAmount
    discountedRate =  originalTotalAmt == 0 ? 0 : (discountedAmount / originalTotalAmt) * 100
    originalPrice = num == 0 ? 0 : originalTotalAmt / num
  }
  record.discountedRate = keepTwoDecimal(discountedRate);
  record.partSubtotalAmount = keepTwoDecimal(totalAmount);
  record.partOriginalTotalAmt = keepTwoDecimal(originalTotalAmt)
  record.discountedAmount = keepTwoDecimal(new Decimal(parseFloat(record.partOriginalTotalAmt) ).sub(new Decimal(parseFloat(record.partSubtotalAmount))).toNumber());
  record.sellPartPrice = keepTwoDecimal(price);
  record.originalPartPrice = keepTwoDecimal(originalPrice < price ? price : originalPrice)// 保持原价不能小于销价
  console.log(record, 'part record');
}

export const changePriceByParent = (record) => {
  let items = record.itemQuoteList ||record.itemList ||[]
  let parts = record.partQuoteList || record.partList || []
  let totalAmount = parseFloat(record.pkgSubtotalAmount || 0); // 销售总价
  let originalTotalAmt = 0
  
  record.priceMethod = 2
  items.forEach((item) => {
    originalTotalAmt += parseFloat(item.itemOriginalTotalAmt)
  })
  parts.forEach((item) => {
    originalTotalAmt += parseFloat(item.partOriginalTotalAmt)
  })
  
  parseFloat(record.sellAmount) // 原始总价
  items.forEach((item) => {
    // 计算每一项的总价、优惠率、优惠金额、单价
    let rate = originalTotalAmt ? item.itemOriginalTotalAmt / originalTotalAmt : 0;
    item.itemSubtotalAmount = keepTwoDecimal(rate * totalAmount); // 总价
    item.discountedAmount = keepTwoDecimal(item.itemOriginalTotalAmt - item.itemSubtotalAmount); // 优惠金额
    let sellItemPrice = keepTwoDecimal(item.itemTime ? item.itemSubtotalAmount  / item.itemTime: 0); // 单价
    if(item.itemSubtotalAmount > item.itemOriginalTotalAmt) { // 保持原价>=销价
      item.originalItemPrice = item.sellItemPrice = sellItemPrice;
      item.discountedAmount = 0;
      item.itemOriginalTotalAmt = item.itemSubtotalAmount;
    }
    // item.sellItemPrice = keepTwoDecimal(item.itemTime ? item.itemSubtotalAmount  / item.itemTime: 0); // 单价
  })
  
  parts.forEach((item) => {
    // 计算每一项的总价、优惠率、优惠金额、单价
    let rate = originalTotalAmt ? item.partOriginalTotalAmt / originalTotalAmt : 0;
    item.partSubtotalAmount = keepTwoDecimal(rate * totalAmount); // 总价
    item.discountedAmount = keepTwoDecimal(item.partOriginalTotalAmt - item.partSubtotalAmount); // 优惠金额
    if(item.partSubtotalAmount > item.partOriginalTotalAmt) { // 保持原价>=销价
      item.discountedAmount = 0;
      item.partOriginalTotalAmt = item.partSubtotalAmount;
    }
    item.sellPartPrice = keepTwoDecimal(item.partQty ? item.partOriginalTotalAmt  / item.partQty: 0); // 单价
  })
  let sellAmount =parseFloat(record.sellAmount || 0)
  let discountedAmount = sellAmount - totalAmount;
  record.discountedAmount = keepTwoDecimal(discountedAmount)
  record.discountedRate = keepTwoDecimal(sellAmount ? (discountedAmount / sellAmount) * 100 : 0)
}

export const changePrentPrice = (record) => {
  record.priceMethod = 3
  let items = record.itemQuoteList || record.itemList || []
  let parts = record.partQuoteList || record.partList || []
  let discountedAmount = 0,
    sellAmount = 0,
    pkgSubtotalAmount = 0;
    console.log(items, parts)
  items.forEach((item) => {
    discountedAmount += parseFloat(item.discountedAmount)
    pkgSubtotalAmount += parseFloat(item.itemSubtotalAmount);
    sellAmount += parseFloat(item.itemOriginalTotalAmt);
  })
  parts.forEach((item) => {
    discountedAmount += parseFloat(item.discountedAmount)
    pkgSubtotalAmount += parseFloat(item.partSubtotalAmount)
    sellAmount += parseFloat(item.partOriginalTotalAmt);
  })
  console.log(pkgSubtotalAmount, sellAmount, discountedAmount)
  record.discountedAmount = keepTwoDecimal(discountedAmount)
  record.pkgSubtotalAmount = keepTwoDecimal(pkgSubtotalAmount)
  record.originalSellAmount = keepTwoDecimal(sellAmount)
  record.originalTotalAmount = keepTwoDecimal(sellAmount)
  // record.sellAmount = keepTwoDecimal(sellAmount)
  record.discountedRate = keepTwoDecimal(sellAmount ? (discountedAmount / sellAmount) * 100 : 0)
}